package Starter;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.util.ArrayList;
import java.util.Vector;

import Components.GameWindow;
import Components.RoomsPanel;

import smash_tris.DataPacket;
import smash_tris.Player;
import smash_tris.Room;


public class BroadcastListener implements Runnable {

	private ObjectInputStream broadcastStreamIn = null;
	private boolean isRunning = false;
	private RoomsPanel roomsPanel = null;
	private GameWindow gameWindow;
	//private LoginPane loginPane;
	private MainFrame mainFrame = null;
	

	/**
	 * Konstruktor
	 * 
	 * @param broadcastStream
	 *            strumień danych broadcastowych.
	 */
	public BroadcastListener(ObjectInputStream broadcastStream) {
		this.broadcastStreamIn = broadcastStream;
		this.isRunning = true;
		Thread t = new Thread(this);
		t.start();
	}
	
	public void setGameWindow(GameWindow gameWindow) {
		this.gameWindow = gameWindow;
	}

	public void setMainFrame(MainFrame mainFrame) {
		this.mainFrame = mainFrame;
	}
	
	@Override
	public void run() {
		System.out.println("BROADCAST___!!!");
		DataPacket userInput = null;
		while (isRunning) {
			try {
				userInput = (DataPacket) broadcastStreamIn.readObject();
				
				switch (userInput.getTypeOfInformation()) {
					case RESET_ROOM_STATE:
						System.out.println(
								"broadcast...");
						if (this.roomsPanel!=null) {
							@SuppressWarnings("unchecked")
							Vector<Room> rooms = (Vector<Room>) userInput
								.getData();
							
							this.roomsPanel.setRooms(rooms);
							System.out.println("--broadcast--------------------------------");
							System.out.println("nazwa\tliczba graczy");
							System.out.println("-------------------------------------------");
							for (Room room: rooms){
								System.out.print(room.getName()+"\t");
								for (Player p: room.getPlayers()){
								 System.out.print( p.getNick() + " : " );
								}
							}
							System.out.println("-------------------------------------------");
						}
						break;
					case ADD_ROOM:
						break;
					case ADD_PLAYER:
						System.out.println("ADD_PLAYER Broadcast!!!");
						break;
					case START_GAME:
						if (gameWindow != null){
							gameWindow.getTetrisPlayground().getMyTetris().start();
							System.out.println("Start");
							
						}
						break;
					case EXIT_FROM_ROOM:
						if (gameWindow != null) {
							System.out.println("Exit from room broadcast");
						//	gameWindow.closeWindow();
						/*	gameWindow.getTetrisPlayground().getTetris().stop(true);
							gameWindow.getTetrisPlayground().getEnemy().stop(true);
							gameWindow.getTetrisPlayground().getTetris().stopThread();
							gameWindow.getTetrisPlayground().getEnemy().stopThread();
							*/
						/*	
							gameWindow.setVisible(false);
							gameWindow.setEnabled(false);
						*/	
							if (gameWindow != null) {
								
								//gameWindow.setSecondary(true);
								gameWindow.dispose();
								//gameWindow.setSecondary(false);
								
								mainFrame.setVisible(true);
								mainFrame.setEnabled(true);
							}
							
						}
						break;
				}
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (ClassNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}

	/**
	 * Metoda przypisuje listenerowi panel, który ma być przez niego
	 * obsługiwany.
	 * 
	 * @param roomsPanel
	 */
	public void setRoomsPanel(RoomsPanel roomsPanel) {
		this.roomsPanel = roomsPanel;
	}
}
